import uuidv4 from 'uuid/v4';
import _ from 'lodash';

const whereMethods = 'where,andWhere,orWhere,whereNot,orWhereNot,whereIn,orWhereIn,whereNotIn,orWhereNotIn,whereNotNull,orWhereNotNull,whereBetween,orWhereBetween,whereNotBetween,orWhereNotBetween'.split(',');
const isWhereMixed = (obj) => {
    return _.isPlainObject(obj) && _.every(obj, (v, k) => _.includes(whereMethods, k));
};
const isWhereArrayMixed = (obj) => {
    return _.isArray(obj) && _.every(obj, v => isWhereMixed(v));
};
export function whereMixed(qb, arr) {
    arr = _.castArray(arr);
    _.each(arr, whs => {
        //是否是高级查询
        if (isWhereMixed(whs)) {
            _.each(whs,
                (v, k) => {
                    if (isWhereMixed(v) || isWhereArrayMixed(v)) {
                        qb[k](qb2 => whereMixed(qb2, v));
                    } else {
                        if (!_.includes(whereMethods, k)) {
                            throw new Error(`where高级查询中出现未知操作方法:${k}`);
                        }
                        if (_.isPlainObject(v)) {
                            qb[k](v);
                        } else {
                            qb[k](...v);
                        }
                    }
                },
            );
        } else {
            qb.where(whs);
        }
    });
}

/**
 * 返回去掉dash的uuid
 * @returns {string} uuid string with dash removed
 */
export function uuid() {
    return uuidv4.v4().replace(/-/g, '');
}

export function andWhere(query, op, cb) {
    query.forEach(item => {
        if (op === 0) {
            cb.where(item);
        } else {
            cb.where(item[0], item[1], item[2]);
        }
    });
    return cb;
}
export function toJson(str) {
    return JSON.parse(JSON.stringify(str));
}
export function resolve() {
    return Promise.resolve();
}
export function reject() {
    return Promise.reject();
}
export function data(data, opt) {
    if (opt) {
        return {
            pagination: {
                page: data.pagination.page,
                pageSize: data.pagination.pageSize,
                total: data.pagination.rowCount,
                pageCount: data.pagination.pageCount
            },
            data: data.toJSON()
        };
    }
    return data.toJSON();
}
export function result(r, d, m, e) {
    /*const _ = {
     result: r,
     data: d,
     message: m,
     err: e,
     };
     return _;*/
    if (r) {
        return d;
    }
    throw e;
}
/**
 * 组装返回给客户端分页数据的格式
 * @param data  array, 数据
 * @param page  页码
 * @param pageSize  每页数量
 * @param dataCount  数据总数
 */
function pageResult(data, page, pageSize, dataCount) {
    return {
        data,
        pagination: {
            page,
            pageSize,
            total: dataCount,
            pageCount: Math.ceil(dataCount / pageSize)
        }
    };
}
/**
 * 将日期格式化输出为字符串， 输出格式为 YYYY-MM-DD hh:mm:ss
 * @param dt
 * @returns {string}
 */
function formatDateTime(dt, withSeconds = false) {
    if (dt && dt.getFullYear) {
        let s = '';
        const y = dt.getFullYear();
        const m = dt.getMonth() + 1;
        const d = dt.getDate();
        const hh = dt.getHours();
        const mm = dt.getMinutes();
        const ss = dt.getSeconds();
        s = y + (m > 9 ? '-' : '-0') + m + (d > 9 ? '-' : '-0') + d + ' ';// eslint-disable-line
        s = s + ( hh > 9 ? '' : '0') + hh + ':' + ( mm > 9 ? '' : '0') + mm;// eslint-disable-line
        if (withSeconds) {
            s += ':' + (ss > 9 ? '' : '0') + ss;// eslint-disable-line
        }
        return s;
    }
    return dt;
}
/**
 * 返回指定位数的随机数字符串
 *
 * @param length 随机字符串的长度
 * @returns {String}
 */
function randomDigits(length) {
    let r = Math.random() * Math.pow(10, length);// eslint-disable-line
    r = String(Math.floor(r));
    for (; r.length < length;) {
        r = '0' + r;// eslint-disable-line
    }
    return r;
}
/**
 * 空函数
 */
function noop() {

}
export {pageResult, formatDateTime, randomDigits, noop};
